#include "include.h"

#ifdef USE_QT

#include "qt_graphicpainter.h"


#include "compile/debug_memoryleaks"

using namespace gui;
using namespace qtgui;

GraphicPainterQT* GraphicPainterQT::create()
{
    return new GraphicPainterQT();
}



GraphicPainterQT::GraphicPainterQT()
: qt_widget(NULL), current_pos(0,0,0)
{
}


GraphicPainterQT::~GraphicPainterQT()
{
    if (qt_painter.isActive())
        qt_painter.end();
}



void GraphicPainterQT::qt_begin_painter()
{
    if (qt_painter.isActive())
        qt_end_painter();

    if (qt_widget)
        qt_painter.begin(qt_widget);
}

void GraphicPainterQT::qt_end_painter()
{
    qt_painter.end();
}

bool GraphicPainterQT::qt_painter_active()
{
    return (qt_painter.isActive());
}











///////////////////




void GraphicPainterQT::moveTo(int x, int y)
{
    current_pos.set(x,y,0);
}


void GraphicPainterQT::lineTo(int x, int y)
{
    Vector to_pos(x,y,0);
	drawLine(current_pos, to_pos);
	current_pos = to_pos;
}

/**
 * Draws a grid point at (x1, y1).
 */
void GraphicPainterQT::drawGridPoint(const Vector& p) {
    qt_painter.drawPoint(toScreenX(p.x), toScreenY(p.y));
}



/**
 * Draws a point at (x1, y1).
 */
void GraphicPainterQT::drawPoint(const Vector& p) {
    qt_painter.drawLine(toScreenX(p.x-1), toScreenY(p.y),
                       toScreenX(p.x+1), toScreenY(p.y));
    qt_painter.drawLine(toScreenX(p.x), toScreenY(p.y-1),
                       toScreenX(p.x), toScreenY(p.y+1));
}



/**
 * Draws a line from (x1, y1) to (x2, y2).
 */
void GraphicPainterQT::drawLine(const Vector& p1, const Vector& p2) {
#ifdef __APPLE__
	int w2 = (int)getPen().getScreenWidth()/2;
    qt_painter.drawLine(toScreenX(p1.x-w2), toScreenY(p1.y-w2),
                       toScreenX(p2.x-w2), toScreenY(p2.y-w2));
#else
    qt_painter.drawLine(toScreenX(p1.x), toScreenY(p1.y),
                       toScreenX(p2.x), toScreenY(p2.y));
#endif
}





#endif
